package com.bite.java_chatroom.controller;

import com.bite.java_chatroom.model.MessageSession;
import com.bite.java_chatroom.model.UserInfo;
import com.bite.java_chatroom.service.MessageService;
import com.bite.java_chatroom.service.MessageSessionService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/message_session")
public class MessageSessionController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private MessageSessionService messageSessionService;

    @ResponseBody
    @RequestMapping("/getsessionlist")
    public List<MessageSession> getMessageSession(HttpSession session){
        List<MessageSession> messageSessionList = new ArrayList<>();
        //处理空指针
        if(session == null){
            log.info("[getMessageSession]: session == null");
            return messageSessionList;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null){
            log.info("[getMessageSession]: userinfo == null");
            return messageSessionList;
        }
        //先获取会话ids
        List<Integer> sessionId = messageSessionService.getSessionIdByUserId(userInfo.getId());
        //遍历会话，找出会话中的friends
        for (Integer id : sessionId){
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(id);
            List<UserInfo> friends = messageSessionService.getFriendsBySessionId(id,userInfo.getId());
            messageSession.setFriends(friends);
            //获取最后一条消息
            String lastMessage = messageService.getLastMessage(id);
            if(lastMessage == null){
                messageSession.setLastMessage("");
            }else {
                messageSession.setLastMessage(lastMessage);
            }
            messageSessionList.add(messageSession);
        }
        return messageSessionList;
    }

    @Transient
    @ResponseBody
    @RequestMapping("/addsession")
    public Integer addMessageSession(Integer friendId, HttpSession session){
        //处理空指针
        if(session == null){
            log.info("[getMessageSession]: session == null");
            return null;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null){
            log.info("[getMessageSession]: userinfo == null");
            return null;
        }
        MessageSession messageSession = new MessageSession();
        //添加会话
        messageSessionService.addMessageSession(messageSession);
        //添加会话与用户关系表
        messageSessionService.addMessageSessionUser(messageSession.getSessionId(),userInfo.getId());
        messageSessionService.addMessageSessionUser(messageSession.getSessionId(),friendId);
        return messageSession.getSessionId();
    }


    @RequestMapping("/getsessionid")
    public Integer getSessionIdByfriendId(Integer friendId, HttpSession session){
        //处理空指针
        if(session == null){
            log.info("[getSessionIdByfriendId]: session == null");
            return null;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null){
            log.info("[getSessionIdByfriendId]: userinfo == null");
            return null;
        }
        log.info("[getSessionIdByfriendId] userinfo userId = "+userInfo.getId());
        log.info("[getSessionIdByfriendId] userinfo friendId = "+friendId);
        return messageSessionService.getSessionIdByfriendId(userInfo.getId(),friendId);
    }
}
